const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 299,
        fixedAtt: 19,
        fixdefence: 23,
        reaction : 23,
        attRate: 5.8, 
        hpRate: 5.8, 
        criticalRate: 3.9,
        criticalStrike: 7.8,
        defRate: 7.3,
        charging: 6.5
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid


const CONFIG = {
    chance: 15,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        // criticalStrike: 62.2,
        // dmgRate: 46.6,
        attRate: 46.6 * 3
    },
    clips: ["attRate", "reaction", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "fixedAtt", "criticalRate", "criticalStrike"],
        ["charging", "fixedAtt", "criticalRate", "criticalStrike"],
        ["charging", "fixedAtt", "criticalRate", "criticalStrike"],
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 0, attRate: 18 * 2, dmgZhi: 0 }   
    ]
}

let charaters = {
    units : {
        name: "申鹤",
        reaction: 0,
        charging: 0,
        defRate: 0,
        defence: 830,
        hp:12993,
        base: 304,    
        criticalRate: 28.8,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

    // 云先森 734    30%
    // 13q  68.34% + (2.5/5/7.5/11.5)
    // 普攻增伤2% 叠10次 
    // 结晶 防御+20%
    // q攻速+12


    // 普攻倍率  97.92 92.31 119.68 59.25 59.25 156.4      218.77    315.64
    // 10级e  243.36(短) 339.84(长) 伤害值+76.75%攻击力 5/7次 持续10/15s  cd10/15s
    // 13级e  287.3(短) 401.2(长) 伤害值+90.61%攻击力 5/7次 
    // 10级q  181.44%+59.62%*n  12s cd20  80能量  15减防(冰 物)
    // 13级q  181.44%+59.62%*n  12s cd20  80能量  
    // q范围内上场角色 冰伤+10%
    // 点按e(提搞普攻重击下落攻击 10s)和蓄力e(提供eq 15s)增伤15%
    // 1命 e使用次数+1
    // 2命 q时间延长6s 场上角色冰爆伤+15
    // 3命 e+3
    // 4命 冰羽效果触发时叠加层数 每层增伤申鹤的e5% 最高50层(即250%)(不知道深渊换房间换层这个效果会不会重置 猜测跟雷神愿力一样会重置)
    // 5命 q+3
    // 6命 由普攻重击触发的冰羽不减少次数 意味着神里冰伤普攻和甘于蓄力射击无限吃冰羽


    // 辅助：
    // 辅助效果  eq或者天赋1 增伤15%
    // 冰增伤10  降冰抗,物抗15
    // 冰伤的伤害值增加 3500 ~ 4500 (5/7)次

    // 1命多一个e  冰羽效果触发次数*2   (不错)
    // 2命冰爆伤+15
    // 满命普攻重击无限触发冰羽
calc(calc_clips_initial());
function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
            count++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + 4780;
            
            if(weapon.skill) { weapon.skill(data); }
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            let dmgZhi = attack * 0.9702;
            data.dmgZhi += dmgZhi;

            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }

            data.dmgRate += 10;

            let EDmg = getDmg({ data, rate:3.3984}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let EDmgRea = getDmg({ data, rate:3.3984, rea}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let QDmg = getDmg({ data, rate:1.8144+0.5962*12}).qw * reduceResistanceK(-5) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }
            let point = dmgZhi;
            // let point = EDmgRea * 1 + QDmg * 1 + (data.phyDmg||0);
            return {point, data, second, attack, msg:{EDmg,EDmgRea,QDmg,dmgZhi}}
        }))
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[1]);
    console.log(charactersDmg.map(v=>{
        return  `得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
